package v1

import (
	"server/common"
	"server/internal/model"
	"server/logger"
	"server/pkg/jenkins"
	"time"
)

type TestBed struct {
	Name   string
	Status string
}

func RunTaskGroup() {

}

func RunPipeline(name string, param map[string]interface{}) {
	pipe := model.PipeApi{}
	pipe.Name = name
	pipe.Info()
	pipe.Status = common.RUNNING
	his_pipe := model.HisPipe{}
	his_pipe.ID = pipe.ID
	his_pipe.Status = common.RUNNING
	his_pipe.Status = common.SUCCESS
	//taskgroups := pipe.TaskGroups
	//for _, group := range taskgroups {
	//	for _, task := range group.Tasks {
	//		fmt.Println(task)
	//	}
	//}

	// 生成历史流水线

	// 遍历流水线任务，运行任务

	//后端监控任务的运行状态
	//job := TaskJob{TaskID: 1, Name: name, Job: jenkins.JenJobs{
	//	Name: name, ID: 1,
	//}}
	//go job.BindTaskStatus()
}

type TaskJob struct {
	Name   string
	TaskID int
	Task   string
	Job    jenkins.JenkinsJob
}

func (task *TaskJob) BindTaskStatus() {
	for {
		task_status, _ := task.Job.JobStatus()
		if task_status != common.RUNNING {
			t := model.HisTask{}
			t.ID = task.TaskID
			t.StopTime = time.Now().Format("2006-01-02 15:04:05")
			t.Status = task_status
			if err := t.Update(); err != nil {
				logger.Error(err.Error())
			}
		}
		time.Sleep(15 * time.Second)
	}
}
